//
//  OrderInfonCell.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/7/29.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "OrderInfonCell.h"

#import "BankCardManageViewController.h"
@interface OrderInfonCell()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *payedButton;
@property (weak, nonatomic) IBOutlet UIButton *notPayButton;
@property (weak, nonatomic) IBOutlet UITextField *orderNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *companyTextField;


@end
@implementation OrderInfonCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.orderNumberTextField.delegate = self;
    self.companyTextField.delegate = self;
    
    [self.orderNumberTextField addTarget:self action:@selector(orderNumberTextChangeEnd) forControlEvents:UIControlEventEditingDidEnd];
    [self.companyTextField addTarget:self action:@selector(companyTextChangeEnd) forControlEvents:UIControlEventEditingDidEnd];
}
- (void)orderNumberTextChangeEnd{
    NSString *orderNumber = [self disable_emoji:self.orderNumberTextField.text];
    if (self.orderNumberTextChangedClick) {
        self.orderNumberTextChangedClick(orderNumber);
    }
}
- (void)companyTextChangeEnd{
    NSString *companyName = [self disable_emoji:self.companyTextField.text];
    if (self.companyTextChangedClick) {
        self.companyTextChangedClick(companyName);
    }
}
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    
    static NSString *ID = @"OrderInfonCell";
    
    OrderInfonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)setModel:(OrderInfoModel *)model{
    if (model.isPay == YES) {
        self.payedButton.selected = YES;
        self.notPayButton.selected = NO;
    } else {
        self.payedButton.selected = NO;
        self.notPayButton.selected = YES;
    }
    self.orderNumberTextField.text = model.orderNumber;
    self.companyTextField.text = model.companyName;
}
- (IBAction)payedButtonClick:(UIButton *)sender {
    self.payedButton.selected = YES;
    self.notPayButton.selected = NO;
    if (self.payStatusChangedClick) {
        self.payStatusChangedClick(YES);
    }
}
- (IBAction)notPayButtonClick:(UIButton *)sender {
    self.payedButton.selected = NO;
    self.notPayButton.selected = YES;
    if (self.payStatusChangedClick) {
        self.payStatusChangedClick(NO);
    }
}

// 获取view所在的控制器
- (UIViewController *)viewInViewController {
    
    UIResponder *resp = [self nextResponder];
    while (resp) {
        if ([resp isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)resp;
        }
        resp = [resp nextResponder];
    }
    return nil;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
